﻿using Microsoft.Extensions.Logging;
using XYAC.Framework.WohuaMesExtend.IHI.LocalData.Context;
using XYAC.Framework.WohuaMesExtend.IHI.LocalData.Models;
using XYAC.Framework.WohuaMesExtend.IHI.Models;
using XYAC.Framework.WohuaMesExtend.IHI.OracleRemoteDB;

namespace XYAC.Framework.WohuaMesExtend.IHI.Services
{
    internal class YB_PROCESS_STEPService : IDisposable
    {

        private const string __REMOTE_SYNC_STATE_PENDING = "PENDING";
        private const string __REMOTE_SYNC_STATE_COMPLETE = "COMPLETE";
        private const string __REMOTE_SYNC_STATE_PLANED = "PLANED";

        public YB_PROCESS_STEPService(OracleMesContext mesDb, LocalDataContext localDb, ILogger<YB_TechnologyService> logger)
        {
            this.MesDb = mesDb;
            this.LocalDb = localDb;
            this.Logger = logger;
        }

        public OracleMesContext MesDb { get; }
        public LocalDataContext LocalDb { get; }
        public ILogger<YB_TechnologyService> Logger { get; }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        ~YB_PROCESS_STEPService()
        {
            GC.SuppressFinalize(this);
        }
        public YB_PROCESS_STEP From(DwlData data)
        {
            return new YB_PROCESS_STEP
            {
                RECORD1 = data.ID.ToString(),
                RECORD_DATE = data.DTime,
                RECORD2 = data.MSecond.ToString(),
                PART_NUMBER = data.SpecNo.ToString(),
                RECORD5 = data.UpSd.ToString(),
                RECORD6 = data.UpTol.ToString(),
                RECORD7 = data.UpOpVal.ToString(),
                RECORD8 = data.UpMidVal.ToString(),
                RECORD9 = data.UpDrVal.ToString(),
                RECORD10 = data.DownSd.ToString(),
                RECORD11 = data.DownTol.ToString(),
                RECORD12 = data.DownOpVal.ToString(),
                RECORD13 = data.DownMidVal.ToString(),
                RECORD14 = data.DownDrVal.ToString(),
                RECORD15 = data.JuanHao.ToString(),
                RECORD16 = data.IsChengeSpeed.ToString(),
                RECORD17 = data.SystemState,
                EQP = data.MachineNo,
                PLAN_ID = data.PlanNo,
                RECEIVE_FLAG = data.Status.ToString(), // 默认值 'PENDING'
                RECORD29 = data.TableName, // 字面值
                RECORD19 = data.Group
                //RECORD30 = data.Sn
            };
        }
        public YB_PROCESS_STEP From(LaserData data)
        {
            return new YB_PROCESS_STEP
            {
                RECORD1 = data.ID.ToString(),
                RECORD_DATE = data.DTime,
                RECORD56 = data.Group,

                PART_NUMBER = data.TypeName.ToString(),
                //PART_NUMBER = data.SpecNo.ToString(),
                RECORD4 = data.SdVal.ToString(),
                RECORD5 = data.Tol.ToString(),
                RECORD6 = data.OpsVal.ToString(),
                RECORD7 = data.OpVal.ToString(),
                RECORD8 = data.MidVal.ToString(),
                RECORD9 = data.DrVal.ToString(),
                RECORD10 = data.DrsVal.ToString(),
                RECORD11 = data.SdWidth.ToString(),
                RECORD12 = data.AllWidth.ToString(),
                RECORD13 = data.WidthTol.ToString(),
                RECORD14 = data.JuanHao.ToString(),
                RECORD15 = data.IsChengeSpeed.ToString(),
                RECORD16 = data.DBW30.ToString(),
                RECORD17 = data.DBW32.ToString(),
                RECORD18 = data.DBW34.ToString(),
                RECORD19 = data.DBW36.ToString(),
                RECORD20 = data.DBW38.ToString(),
                RECORD21 = data.DBW40.ToString(),
                RECORD22 = data.DBW42.ToString(),
                RECORD23 = data.DBW44.ToString(),
                RECORD24 = data.DBW46.ToString(),
                RECORD25 = data.DBW48.ToString(),
                RECORD26 = data.DBW50.ToString(),
                RECORD27 = data.DBW52.ToString(),
                RECORD29 = data.TableName.ToString(), // 字面值
                EQP = data.MachineNo,
                //PLAN_ID = data.PlanNo,
                RECEIVE_FLAG = data.Status.ToString(), // 字面值
                //RECORD30 = data.Sn
            };
        }


        public DwlData ToDwlData(YB_PROCESS_STEP data)
        {
            return new DwlData
            {
                ID = data.RECORD1 == null ? 0 : Convert.ToInt32(data.RECORD1),
                DTime = data.RECORD_DATE,
                MSecond = data.RECORD2 == null ? (short)0 : Convert.ToInt16(data.RECORD2),
                //Group = data.RECORD3.HasValue,
                TypeName = data.PART_NUMBER,
                UpSd = data.RECORD5 == null ? 0f : Convert.ToInt16(data.RECORD5),
                UpTol = data.RECORD6 == null ? 0f : Convert.ToInt16(data.RECORD6),
                UpOpVal = data.RECORD7 == null ? 0f : Convert.ToInt16(data.RECORD7),
                UpMidVal = data.RECORD8 == null ? 0f : Convert.ToInt16(data.RECORD8),
                UpDrVal = data.RECORD9 == null ? 0f : Convert.ToInt16(data.RECORD9),
                DownSd = data.RECORD10 == null ? 0f : Convert.ToInt16(data.RECORD10),
                DownTol = data.RECORD11 == null ? 0f : Convert.ToInt16(data.RECORD11),
                DownOpVal = data.RECORD12 == null ? 0f : Convert.ToInt16(data.RECORD12),
                DownMidVal = data.RECORD13 == null ? 0f : Convert.ToInt16(data.RECORD13),
                DownDrVal = data.RECORD14 == null ? 0f : Convert.ToInt16(data.RECORD14),
                JuanHao = data.RECORD15 == null ? 0 : Convert.ToInt32(data.RECORD15),
                IsChengeSpeed = data.RECORD16 == null ? 0 : Convert.ToInt32(data.RECORD16),
                SystemState = data.RECORD56,
                MachineNo = data.EQP,
                PlanNo = data.PLAN_ID,
                SpecNo = data.PART_NUMBER,
                Status = data.RECEIVE_FLAG, // 通常为 'PENDING' 或 'COMPLETE'
                Sn = data.RECORD19.ToString(),
            };
        }
        public LaserData ToLaserData(YB_PROCESS_STEP data)
        {
            return new LaserData
            {
                ID = data.RECORD1 == null ? 0 : Convert.ToInt32(data.RECORD1),
                DTime = data.RECORD_DATE,
                Group = data.RECORD56,
                TypeName = data.PART_NUMBER,
                SdVal = (data.RECORD4 == null) ? 0f : Convert.ToSingle(data.RECORD4),
                Tol = (data.RECORD5 == null) ? 0f : Convert.ToSingle(data.RECORD5),
                OpsVal = (data.RECORD6 == null) ? 0f : Convert.ToSingle(data.RECORD6),
                OpVal = (data.RECORD7 == null) ? 0f : Convert.ToSingle(data.RECORD7),
                MidVal = (data.RECORD8 == null) ? 0f : Convert.ToSingle(data.RECORD8),
                DrVal = (data.RECORD9 == null) ? 0f : Convert.ToSingle(data.RECORD9),
                DrsVal = (data.RECORD10 == null) ? 0f : Convert.ToSingle(data.RECORD10),
                SdWidth = (data.RECORD11 == null) ? 0f : Convert.ToSingle(data.RECORD11),
                AllWidth = (data.RECORD12 == null) ? 0f : Convert.ToSingle(data.RECORD12),
                WidthTol = (data.RECORD13 == null) ? 0f : Convert.ToSingle(data.RECORD13),
                JuanHao = (data.RECORD14 == null) ? 0 : Convert.ToInt32(data.RECORD14),
                IsChengeSpeed = (data.RECORD15 == null) ? 0 : Convert.ToInt32(data.RECORD15),
                DBW30 = (data.RECORD16 == null) ? 0f : Convert.ToSingle(data.RECORD16),
                DBW32 = (data.RECORD17 == null) ? 0f : Convert.ToSingle(data.RECORD17),
                DBW34 = (data.RECORD18 == null) ? 0f : Convert.ToSingle(data.RECORD18),
                DBW36 = (data.RECORD19 == null) ? 0f : Convert.ToSingle(data.RECORD19),
                DBW38 = (data.RECORD20 == null) ? 0f : Convert.ToSingle(data.RECORD20),
                DBW40 = (data.RECORD21 == null) ? 0f : Convert.ToSingle(data.RECORD21),
                DBW42 = (data.RECORD22 == null) ? 0f : Convert.ToSingle(data.RECORD22),
                DBW44 = (data.RECORD23 == null) ? 0f : Convert.ToSingle(data.RECORD23),
                DBW46 = (data.RECORD24 == null) ? 0f : Convert.ToSingle(data.RECORD24),
                DBW48 = (data.RECORD25 == null) ? 0f : Convert.ToSingle(data.RECORD25),
                DBW50 = (data.RECORD26 == null) ? 0f : Convert.ToSingle(data.RECORD26),
                DBW52 = (data.RECORD27 == null) ? 0f : Convert.ToSingle(data.RECORD27),
                MachineNo = data.EQP,
                PlanNo = data.PLAN_ID,
                SpecNo = data.PART_NUMBER,
                Status = data.RECEIVE_FLAG,
                TableName = data.RECORD29.ToString(),
                Sn = data.RECORD30.ToString(),
            };
        }

        internal int Add(YB_PROCESS_STEP insertData)
        {
            this.MesDb.Add(insertData);
            return this.MesDb.SaveChanges();
        }



        /// <summary>
        /// 更新本地数据库中间表状态为1,上传生产数据至mes库
        /// </summary>
        /// <param name="dwlData"></param>
        /// <param name="laserData"></param>
        /// <param name="inspectModel"></param>
        /// <returns></returns>
        internal int SyncRemoteProduceData(DwlData dwlData, LaserData laserData, YB_LOT_INSPECT_Local inspectModel)
        {
            int ret = 0;
            dwlData.PlanNo = inspectModel.PLANID;
            dwlData.Sn = inspectModel.SERIAL_NUMBER;
            dwlData.MachineNo = inspectModel.EQP;

            laserData.PlanNo = inspectModel.PLANID;
            laserData.Sn = inspectModel.SERIAL_NUMBER;
            laserData.MachineNo = inspectModel.EQP;

            using (var remoteTransaction = this.MesDb.Database.BeginTransaction())
            {
                using (var localTransaction = this.LocalDb.Database.BeginTransaction())
                {
                    try
                    {
                        var dwlDataStep = this.From(dwlData);
                        var laserDataStep = this.From(laserData);
                        //To do
                        //此处有一点需要再次确认,本地生产会产生哪些表数据dwlDataStep|laserDataStep
                        this.MesDb.YB_PROCESS_STEP.Add(dwlDataStep);
                        this.MesDb.YB_PROCESS_STEP.Add(laserDataStep);

                        inspectModel.RemoteSyncState = true;//将本地数据状态RemoteSyncState=true
                        inspectModel.RemoteSyncDate = DateTime.Now;
                        this.LocalDb.YB_LOT_INSPECT.Update(inspectModel);



                        int tempRetRemote = this.MesDb.SaveChanges();
                        int tempRetLocal = this.LocalDb.SaveChanges();
                        if (tempRetLocal == 1 && tempRetRemote == 2)
                        {
                            remoteTransaction.Commit();
                            localTransaction.Commit();
                            ret = tempRetLocal + tempRetRemote;
                        }
                    }
                    catch (Exception)
                    {
                        remoteTransaction.Rollback();
                        localTransaction.Rollback();
                    }
                }
            }
            return ret;

        }


    }
}
